package com.softwinner.awbt.fragment;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.softwinner.awbt.R;
import com.softwinner.awbt.dao.CallLog;

import org.w3c.dom.Text;

import java.util.List;

public class CallLogAdapter extends BaseAdapter {

    private List<CallLog> mValues;
    private Context mContext;

    public CallLogAdapter(Context context, List<CallLog> items) {
        mContext = context;
        mValues = items;
    }

    @Override
    public int getCount() {
        if (mValues == null) {
            return 0;
        }
        return mValues.size();
    }

    @Override
    public CallLog getItem(int i) {
        if (mValues == null) {
            return null;
        }
        return mValues.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.call_log_item, null, false);
            viewHolder = new ViewHolder();
            viewHolder.mTypeView = (ImageView) view.findViewById(R.id.type_iv);
            viewHolder.mNameView = (TextView) view.findViewById(R.id.name_tv);
            viewHolder.mNumberView = (TextView) view.findViewById(R.id.number_tv);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        CallLog callLog = mValues.get(i);
        switch (callLog.getCalltype()) {
            case 4:
                viewHolder.mTypeView.setImageResource(R.drawable.ic_phone_out);
                break;
            case 5:
                viewHolder.mTypeView.setImageResource(R.drawable.ic_phone_in);
                break;
            case 6:
                viewHolder.mTypeView.setImageResource(R.drawable.ic_phone_missed);
                break;
        }

        if (TextUtils.isEmpty(callLog.getPhonename())) {
            viewHolder.mNameView.setText(android.R.string.unknownName);
        } else {
            viewHolder.mNameView.setText(callLog.getPhonename());
        }

        viewHolder.mNumberView.setText(callLog.getPhonenumber());
        return view;
    }

    public void setCallLogs(List<CallLog> callLogs) {
        mValues = callLogs;
        notifyDataSetChanged();
    }


    public class ViewHolder {
        public ImageView mTypeView;
        public TextView mNameView;
        public TextView mNumberView;
    }
}
